package cn.play.cardgame;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;

import java.util.HashMap;
import java.util.Map;

import cn.play.cardgame.util.LogUtil;

public class SoundService extends Service implements MediaPlayer.OnCompletionListener {
    public Map<String, Integer> paiVoiceMap = new HashMap<>();
//	private MediaPlayer mp;
//
//	@Override
//	public void onCreate() {
//		super.onCreate();
//		mp = MediaPlayer.create(this, R.raw.background_music);
//		mp.setLooping(true);
//		mp.start();
//	}
//
//	@Override
//	public void onDestroy() {
//		super.onDestroy();
//		if (mp != null) {
//			mp.release();
//			mp = null;
//		}
//	}
//
//	@Override
//	public IBinder onBind(Intent intent) {
//		return null;
//	}
//
//}

    public static final int PLAY_MSG = 1;
    public static final int PAUSE_MSG = 2;
    public static final int STOP_MSG = 3;
    private MediaPlayer mediaPlayer;
    private MediaPlayer mediaPlayer2;
    // private boolean isPause; //暂停状态
    private final IBinder binder = new SoundBinder();

    private int lastSong = -1;
    // private int lastPos = 0;
    private boolean isFirst = true;

    private Map<Integer, Integer> songMap = new HashMap<Integer, Integer>();

    @Override
    public IBinder onBind(Intent arg0) {
        return binder;
    }

    /**
     * 当Audio播放完的时候触发该动作
     */
    @Override
    public void onCompletion(MediaPlayer player) {
        // stopSelf();//结束了，则结束Service
    }

    // 在这里我们需要实例化MediaPlayer对象
    public void onCreate() {
        super.onCreate();
        // 我们从raw文件夹中获取一个应用自带的mp3文件
        // mediaPlayer = MediaPlayer.create(this, R.raw.background_music);
        // mediaPlayer.setLooping(true);
        // mediaPlayer.setOnCompletionListener(this);
        initData();
    }

    /**
     * 播放音乐
     *
     * @param position
     */
    private void play(int position, int song) {
        try {
//			if (mediaPlayer != null)
//				mediaPlayer.reset();// 把各项参数恢复到初始状态
            // mediaPlayer.setDataSource(path);
            // mediaPlayer.prepare(); // 进行缓冲

            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
            if (song == 0) {
                mediaPlayer = MediaPlayer.create(this, R.raw.background_music);
                mediaPlayer.setLooping(true);
            } else if (song == 1) {
                mediaPlayer = MediaPlayer.create(this, R.raw.background_music);
                mediaPlayer.setLooping(true);
            }
            // mediaPlayer.start();
            mediaPlayer.setOnPreparedListener(new PreparedListener(position));// 注册一个监听器

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 暂停音乐
     */
    private void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            // isPause = true;
        }
    }

    /**
     * 停止音乐
     */
    private void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
//			try {
//				mediaPlayer.prepare(); // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
        }
    }

    @Override
    public void onDestroy() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    /**
     * 实现一个OnPrepareLister接口,当音乐准备好的时候开始播放
     */
    private final class PreparedListener implements MediaPlayer.OnPreparedListener {
        private int positon;

        public PreparedListener(int positon) {
            this.positon = positon;
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
//			if (positon > 0) { // 如果音乐不是从头播放
//				mediaPlayer.seekTo(positon);
//			}
            mediaPlayer.start(); // 开始播放
        }

    } /**
     * 实现一个OnPrepareLister接口,当音乐准备好的时候开始播放
     */
    private final class PreparedListener2 implements MediaPlayer.OnPreparedListener {
        private int positon;

        public PreparedListener2(int positon) {
            this.positon = positon;
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
//			if (positon > 0) { // 如果音乐不是从头播放
//				mediaPlayer.seekTo(positon);
//			}
            mediaPlayer2.start(); // 开始播放
        }

    }

    /**
     * 该方法在SDK2.0才开始有的，替代原来的onStart方法
     */
    public int onStartCommand(Intent intent, int flags, int startId) {
        // if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        // stop();
        // }
        LogUtil.getLogger().d7("SoundService", "---SoundService---onStartCommand-------");
        if (intent == null)
            return super.onStartCommand(intent, flags, startId);
        int msg = intent.getIntExtra("MSG", 0);
        if (msg == PLAY_MSG) {
            int currentSong = intent.getIntExtra("SONG", -1);
            if (isFirst) {
                isFirst = false;
                lastSong = currentSong;
                play(0, currentSong);
            } else {
                if (lastSong != currentSong) {
                    if (mediaPlayer != null)
                        songMap.put(lastSong, mediaPlayer.getCurrentPosition());
                    lastSong = currentSong;
                    stop();
                    // play(0, currentSong);
                    play(songMap.get(currentSong) == null ? 0 : songMap.get(currentSong), currentSong);
                } else {
                    stop();
                    play(songMap.get(currentSong) == null ? 0 : songMap.get(currentSong), currentSong);
                }
            }
        } else if (msg == PAUSE_MSG) {
            pause();
        } else if (msg == STOP_MSG) {
            stop();
        }
        return START_STICKY;
    }

    // 为了和Activity交互，我们需要定义一个Binder对象
    class SoundBinder extends Binder {

        // 返回Service对象
        SoundService getService() {
            return SoundService.this;
        }
    }

    // 后退播放进度
    // public void haveFun() {
    // if (player.isPlaying() && player.getCurrentPosition() > 2500) {
    // player.seekTo(player.getCurrentPosition() - 2500);
    // }
    // }


    public void playSongByType(int type, int index, String huase) {
        switch (type) {
            case 0:
                stop();
                break;
            case 1:
                if (index == -1) {
                    play(0, 1);
                } else {
                    play(index, huase);
                }

                break;
            case 2:
                pause();
                break;
        }
    }


    private void play(int index, String huase) {
        try {
            if(mediaPlayer2!=null){
                mediaPlayer2.stop();
            }
            LogUtil.getLogger().d7("08221455", "huase = " + huase + "_" + (index + 1));
            mediaPlayer2 = MediaPlayer.create(this, paiVoiceMap.get(huase + "_" + (index + 1)));
//            mediaPlayer.setLooping(true);
            mediaPlayer2.setOnPreparedListener(new PreparedListener2(index));// 注册一个监听器
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initData() {
        paiVoiceMap.put("Heixin_1", R.raw.heixin_1);
        paiVoiceMap.put("Heixin_2", R.raw.heixin_2);
        paiVoiceMap.put("Heixin_3", R.raw.heixin_3);
        paiVoiceMap.put("Heixin_4", R.raw.heixin_4);
        paiVoiceMap.put("Heixin_5", R.raw.heixin_5);
        paiVoiceMap.put("Heixin_6", R.raw.heixin_6);
        paiVoiceMap.put("Heixin_7", R.raw.heixin_7);
        paiVoiceMap.put("Heixin_8", R.raw.heixin_8);
        paiVoiceMap.put("Heixin_9", R.raw.heixin_9);
        paiVoiceMap.put("Heixin_10", R.raw.heixin_10);
        paiVoiceMap.put("Heixin_11", R.raw.heixin_11);
        paiVoiceMap.put("Heixin_12", R.raw.heixin_12);
        paiVoiceMap.put("Heixin_13", R.raw.heixin_13);

        paiVoiceMap.put("Hongxin_1", R.raw.hongxin_1);
        paiVoiceMap.put("Hongxin_2", R.raw.hongxin_2);
        paiVoiceMap.put("Hongxin_3", R.raw.hongxin_3);
        paiVoiceMap.put("Hongxin_4", R.raw.hongxin_4);
        paiVoiceMap.put("Hongxin_5", R.raw.hongxin_5);
        paiVoiceMap.put("Hongxin_6", R.raw.hongxin_6);
        paiVoiceMap.put("Hongxin_7", R.raw.hongxin_7);
        paiVoiceMap.put("Hongxin_8", R.raw.hongxin_8);
        paiVoiceMap.put("Hongxin_9", R.raw.hongxin_9);
        paiVoiceMap.put("Hongxin_10", R.raw.hongxin_10);
        paiVoiceMap.put("Hongxin_11", R.raw.hongxin_11);
        paiVoiceMap.put("Hongxin_12", R.raw.hongxin_12);
        paiVoiceMap.put("Hongxin_13", R.raw.hongxin_13);

        paiVoiceMap.put("Fangkuai_1", R.raw.fangkuai_1);
        paiVoiceMap.put("Fangkuai_2", R.raw.fangkuai_2);
        paiVoiceMap.put("Fangkuai_3", R.raw.fangkuai_3);
        paiVoiceMap.put("Fangkuai_4", R.raw.fangkuai_4);
        paiVoiceMap.put("Fangkuai_5", R.raw.fangkuai_5);
        paiVoiceMap.put("Fangkuai_6", R.raw.fangkuai_6);
        paiVoiceMap.put("Fangkuai_7", R.raw.fangkuai_7);
        paiVoiceMap.put("Fangkuai_8", R.raw.fangkuai_8);
        paiVoiceMap.put("Fangkuai_9", R.raw.fangkuai_9);
        paiVoiceMap.put("Fangkuai_10", R.raw.fangkuai_10);
        paiVoiceMap.put("Fangkuai_11", R.raw.fangkuai_11);
        paiVoiceMap.put("Fangkuai_12", R.raw.fangkuai_12);
        paiVoiceMap.put("Fangkuai_13", R.raw.fangkuai_13);

        paiVoiceMap.put("Caohua_1", R.raw.caohua_1);
        paiVoiceMap.put("Caohua_2", R.raw.caohua_2);
        paiVoiceMap.put("Caohua_3", R.raw.caohua_3);
        paiVoiceMap.put("Caohua_4", R.raw.caohua_4);
        paiVoiceMap.put("Caohua_5", R.raw.caohua_5);
        paiVoiceMap.put("Caohua_6", R.raw.caohua_6);
        paiVoiceMap.put("Caohua_7", R.raw.caohua_7);
        paiVoiceMap.put("Caohua_8", R.raw.caohua_8);
        paiVoiceMap.put("Caohua_9", R.raw.caohua_9);
        paiVoiceMap.put("Caohua_10", R.raw.caohua_10);
        paiVoiceMap.put("Caohua_11", R.raw.caohua_11);
        paiVoiceMap.put("Caohua_12", R.raw.caohua_12);
        paiVoiceMap.put("Caohua_13", R.raw.caohua_13);

        paiVoiceMap.put("a_1", R.raw.a_1);
        paiVoiceMap.put("youxi_kaisi_1", R.raw.youxi_kaisi_1);
    }
}
